var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var _dataOAuth = {
		facebook:{
			FACEBOOK_APP_ID:"225598430922182",
			FACEBOOK_APP_SECRET:"974e597849a89c31583113b6d052c388",
			callbackURL:"http://localhost:3000/auth/facebook/callback",
			pathOauth:"",
			pathCallbackOauth:""
		},
		twitter:{
			TWITTER_CONSUMER_KEY:"dgCSStHkKO3IWbMXTIetA",
			TWITTER_CONSUMER_SECRET:"n58sGNde86Qz9Kglv2UA6w10bZVEL0cnarjJCPaXY",
			callbackURL:"http://localhost:3000/auth/twitter/callback"
			
		}
		
};

module.exports = function(app,passport){		
	
	//-------------
	// facebook
	//-------------
	
//	var FACEBOOK_APP_ID = "225598430922182";
//	var FACEBOOK_APP_SECRET = "974e597849a89c31583113b6d052c388";
	passport.serializeUser(function(user, done){ done(null, user);});
	passport.deserializeUser(function(obj, done){ done(null, obj);});
	passport.use(new FacebookStrategy({
	    clientID: _dataOAuth.facebook.FACEBOOK_APP_ID,
	    clientSecret: _dataOAuth.facebook.FACEBOOK_APP_SECRET,
	    callbackURL: _dataOAuth.facebook.callbackURL
	  },
	  function(accessToken, refreshToken, profile, done) {
	    process.nextTick(function () {
	      return done(null, profile);
	    });
	  }
	));
	
	//------------
	// twiter
	//------------
	passport.use(new TwitterStrategy({
	    consumerKey: _dataOAuth.twitter.TWITTER_CONSUMER_KEY,
	    consumerSecret: _dataOAuth.twitter.TWITTER_CONSUMER_SECRET,
	    callbackURL: _dataOAuth.twitter.callbackURL
	  },
	  function(token, tokenSecret, profile, done) {
	    process.nextTick(function () {
	      return done(null, profile);
	    });
	  }
	));
	//----------------------------
	//Paths de autentificacion
	//----------------------------
	
	app.get('/auth/facebook',
			passport.authenticate('facebook'),
		  function(req, res){
		  });
	
	app.get('/auth/facebook/callback', 
			passport.authenticate('facebook', { failureRedirect: '/' }),
	  function(req, res) {
	    res.redirect('/welcome');
	  });
	
	function ensureAuthenticated(req, res, next) {
		  if (req.isAuthenticated()) { return next(); }
		  res.redirect('/');
	}
	
	app.get('/auth/twitter',
			passport.authenticate('twitter'),
		  function(req, res){
		  });
	
	app.get('/auth/twitter/callback', 
			passport.authenticate('twitter', { failureRedirect: '/' }),
	  function(req, res) {
	    res.redirect('/welcome');
	  });

	app.get('/logout', function(req, res){
	  req.logout();
	  res.redirect('/');
	});

	app.get('/welcome',function(req,res){
		if(req.user)
			res.render('welcome',{user:req.user});
		else 
			res.redirect('/');
	});
	
};